<?php

namespace app\admin\controller;

use app\common\controller\Send;
use app\common\enums\StatusEnum;
use think\facade\Request;
use app\admin\model\CustomerService AS CustomerModel;
use app\admin\validate\CustomerService AS CustomerValidate;
use app\admin\model\Oauth as UserModel;

class CustomerService extends Base
{
    use Send;

    /**
     * 列表
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $search = input('');
        $name = isset($search['name']) ? $search['name'] : '';
        $status = isset($search['status']) ? $search['status'] : -1;
        $search = [
            'name' => $name,
            'status' => $status
        ];
        $where = [];
        if ($search['name']) {
            $where[] = ['name', 'like', '%' . $search['name'] . '%'];
        }

        if ($search['status'] != null && $search['status'] != -1) {
            $where[] = ['status', '=', $search['status']];
        }

        $model = CustomerModel::getList($where);
        return view('index', [
            'model' => $model['model'],
            'count' => $model['count'],
            'search' => $search,
            'status' => StatusEnum::$listExplain,
        ]);
    }

    /**
     * 添加修改
     * @return \think\response\Json|\think\response\View
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function form()
    {
        if (Request::instance()->isPost()) {
            $data = input('post.');
            // 检测变量
            $validate = new CustomerValidate();
            if (!$validate->check($data)) {
                return json(self::sendResult(102, [], $validate->getError()));
            }
            $id = UserModel::getId($data['oauth_phone']);
            if (empty($id)) {
                return json(self::sendResult(604, [], $validate->getError()));
            } else {
                $uid = CustomerModel::getUid($id);
                if ($uid) {
                    return json(self::sendResult(605, [], $validate->getError()));
                }
            }
            $data['uid'] = $id;
            //二维码规则
            $data['code'] = substr($data['phone'],5,4).rand(10,99);
            $langId = CustomerModel::saveOne($data);
            $code = 602;
            if ($langId > 0) {
                $code = self::$okCode;
            }
            return json(self::sendResult($code, [
                'url' => url('index'),
            ]));
        } else {
            $id = input('id') ? input('id') : 0;
            $model = [];
            $title = '添加销售人员';
            if ($id > 0) {
                $title = '编辑销售人员';
                $model = CustomerModel::get($id);
                $model['oauth_phone']=  UserModel::getOne($model['uid']) ? UserModel::getOne($model['uid']) : 0;

            }
            return view('form', [
                'status' => StatusEnum::$listExplain,
                'model' => $model,
                'title' => $title
            ]);
        }
    }

    /**
     * 删除
     * @param $id
     * @return array
     */
    public function delete($id)
    {
        CustomerModel::deleteOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    public function deletes()
    {
        $ids = input('ids');
        // 删除数据
        CustomerModel::deleteAll($ids);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }

    /**
     * 修改状态
     * @param $id
     * @return array
     */
    public function disabled($id)
    {
        CustomerModel::disabledOne($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
        ]);
    }
    /**
     * 生成销售二维码
     */
    public function addCode($id) {
        $model = CustomerModel::addCode($id);
        return self::sendResult(self::$okCode, [
            'url' => url('index'),
            'status' => self::$okCode,
            'msg' => $model,
        ]);
    }
    /**
     * 绑定下级
     */
    public function addAgent() {
        $data = input('');
        $code = 602;
        $model = CustomerModel::addAgent($data);

        if ($model == true) {
            $code = self::$okCode;
        }
        return json(self::sendResult($code, [
            'url' => url('index'),
        ]));
//        return self::sendResult(self::$okCode, [
//            'url' => url('index'),
//            'status' => self::$okCode,
//            'msg' => $model,
//        ]);
    }
}
